package com.yc.knn;


import com.yc.bean.BankMarketing;
import com.yc.group2.ParalleGroupKnnClassifier;

import java.util.List;

/**
 * 用线程池方案，减少线程创建
 */
public class Test4_main {
    public static void main(String[] args) {
        String trainpath=System.getProperty("user.dir")+"\\src\\main\\java\\com\\yc\\data\\bank.data";
        List<BankMarketing> train = BankMarketingLoader.load(trainpath); //训练集
        System.out.println("训练集大小："+train.size());
        String testpath=System.getProperty("user.dir")+"\\src\\main\\java\\com\\yc\\data\\bank.test";
        List<BankMarketing> test = BankMarketingLoader.load(testpath); //测试集
        System.out.println("测试集大小："+test.size());

        //knn的k的确定
        int k=10;
        if(args!=null&&args.length>0){
            k=Integer.parseInt(args[0]);
        }

        //定义两个变量存测试集 通过模型预测准确率
        int success=0,mistakes=0;
        int numThreads = Runtime.getRuntime().availableProcessors();
        ParalleGroupKnnClassifier classifier = new ParalleGroupKnnClassifier(k, numThreads, true, train);

        long start,end;
        start=System.currentTimeMillis();

        //循环测试集中的每一条，调用总共模型进行预测
        for(BankMarketing testData:test){
            String tag=classifier.classify(testData);
            if(tag.equals(testData.getTag())){
                success++;
            }else{
                mistakes++;
            }
        }

        end=  System.currentTimeMillis();
        System.out.println("按cpu的核数生成任务的版本：计算时间为："+(end-start)+"ms");
        System.out.println("正确数为："+success+",错误数为："+mistakes+",正确率为："+((double)success/(success+mistakes)));

    }
}



















